<template>
    <div class="app-container">
        <div class="tech-border-out">
            <div class="tech-border">
                <el-card>
                    <div slot="header" class="clearfix">
                        <dv-decoration-7 style="width:150px;height:30px;;"> <p style="padding: 0 15px;">设备分类</p>  </dv-decoration-7>
                    </div>
                    <div class="filter-container">
                            <el-input v-model="listQuery.name" placeholder="请输入分类名称" clearable style="width: 200px;margin-right: 10px;margin-bottom:10px" class="filter-item" @keyup.enter.native="handleFilter" />


                            <el-select v-model.trim="listQuery.pid" placeholder="请选择分类" clearable  filterable  style="width: 200px;margin-right: 10px;margin-bottom:10px" class="filter-item" >
                                <el-option  v-for="(category,index) in category_list" :key="index" :value="category.id" :label="category.html + category.name"></el-option>
                            </el-select>

                        <el-button v-waves  class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
                        {{ '搜索' }}
                        </el-button>
                    <el-button v-waves v-if="checkPermission(['equipment.category.create'])"  class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
                        {{ "添加" }}
                    </el-button>  



                    </div>
                    <el-table
                    :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                    :data="list"
                    size="medium"
                    fit
                    highlight-current-row
                    class="table-container"
                    style="width: 100%"
                    row-key="id"
                    max-height="950"
                    :tree-props="{children: 'children'}"
                        >  
                        
                    <el-table-column  align="left" label="分类名称" >
                        <template slot-scope="scope">
                            <span>{{ scope.row.name }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column  align="center" label="所属分类">
                        <template slot-scope="scope">
                            <span v-if="scope.row.parent_name">{{ scope.row.parent_name }}</span>
                            <span v-else>无</span>
                        </template>
                    </el-table-column>
                   
                    <el-table-column  align="center" label="排序">
                        <template slot-scope="scope">
                            <span>{{ scope.row.sort }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="show" label="是否显示"  align="center"  header-align="center">
                        <template slot-scope="scope">
                                <el-switch v-model="scope.row.status" :active-value='1' :inactive-value='0' @change="switch_status(scope.row)"></el-switch>
                        </template>
                    </el-table-column>

                    <el-table-column  align="center" label="创建时间">
                        <template slot-scope="scope">
                            <span>{{ scope.row.create_time }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" width="250" :label="'操作'">
                        <template slot-scope="scope">
                            
                            <el-button v-waves v-permission="['projects.edit']"  type="primary"  size='mini' icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">
                                {{ "编辑" }}
                            </el-button>
                            <el-button v-waves v-permission="['projects.delete']"  type="danger" :destroy-on-close="true" size='mini' icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)">
                                {{ "删除" }}
                            </el-button>
                        </template>
                    </el-table-column>
                    </el-table>
                    <dv-decoration-10 :dur="1" style="height:5px;" />
                </el-card>
            </div>
        </div>
       
        <el-dialog  width="650px"  :visible.sync="edit.dialogVisible"  :show-close="false" @close="getList" >
            <dv-border-box-11  class="el-new-dialog" title="编辑分类">
                <edit  :category_list="category_list"  :item="edit" />
            </dv-border-box-11>
        </el-dialog>
        <el-dialog width="650px"   :visible.sync="create.dialogVisible" @close="getList"  :show-close="false">
            <dv-border-box-11  class="el-new-dialog" title="添加分类">
                <create :category_list="category_list"  :item="create" />
            </dv-border-box-11>
        </el-dialog>
    </div>
</template>
<script>
    import waves from '@/directive/waves' // waves directive
    import permission from '@/directive/permission/index.js' // 权限判断指令
    import checkPermission from '@/utils/permission' // 权限判断函数
    import { list, detail, del,edit,equipment_switch } from '@/api/equipment_category' // api接口
    import { parseTime } from '@/utils' // 时间格式化函数
    import { getToken } from '@/utils/auth' // 获取token
    import Pagination from '@/components/Pagination' // 分页组件
    import Create from './components/category/create' // 添加组件  
    import Edit from './components/category/edit'  // 编辑组件  

    export default{
        name: 'equipmentCategory',
        components: { Pagination,Create,Edit }, // 引入组件
        filters: {
            parseTime // 时间过滤器
        },
        directives: { waves, permission }, // 引入指令
          data() {
            return {
             headerObj: {
                'Authorization': 'Bearer ' + getToken()
            },
            list: [], // 列表数据
            total: 0, // 总条数
            select_ids : [], // 选中的id集合
            category_list : [], // 分类列表
            listQuery: { //列表查询参数
                page: 1,
                limit: 10
            },
             create: { // 新增
                 dialogVisible: false
                },  
            edit: { // 编辑
                 dialogVisible: false,
                 id: ''
            },  

            }
        },
          created() {
            this.getList()
        },
        methods: {
            checkPermission, // 权限判断函数
            handleFilter() { // 查询
                this.listQuery.page = 1
                this.getList()
            },
            switch_status(row){
                equipment_switch({id:row.id,'status':row.status}).then(res => {
                    this.$message({
                        message: '操作成功',
                        type: 'success'
                    });
                }).catch(err => {
                    this.$message.error(err.message);
                })
            },
            getList() { // 获取列表
                list(this.listQuery).then(response => {
                        this.list = response.data.data
                        this.total = response.data.total
                        this.category_list = response.data.category_list
                    })
                },
        handleCreate() { // 新增对话框显示
            this.create.dialogVisible = true
        },  
        handleEdit(index, row) { // 编辑对话框显示
            this.edit.dialogVisible = true
            this.edit.id = row.id
        },  
        handleDelete(index, row) { // 删除
            const thiz = this
            this.$confirm('确认要删除吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                del(row.id).then(() => {
                this.$message({
                    message: '删除成功',
                    type: 'success',
                    duration: 1500,
                    onClose() {
                        thiz.getList();
                    }
                })
                })
            }).catch(() => {

            })
        },   

            }
    }
</script>
<style scoped> 
    ::v-deep .el-icon-arrow-right{
        color: #fff!important;
    }
</style>

